/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.infosystem.server.dao.xml;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import ru.infosystem.server.dao.DaoFactory;
import ru.infosystem.server.dao.DaoFaculty;
import ru.infosystem.server.dao.DaoGroup;
import ru.infosystem.server.dao.DaoStudent;

/**
 *
 * @author gbuh
 */
public class XmlDaoFactory implements DaoFactory {

    
    private String source = "src/ru/infosystem/server/dao/xml/DataBase.xml";
    
    @Override
    public DaoGroup getGroup() {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(source);
            return new XmlDaoGroup(document);
        } catch (SAXException ex) {
            Logger.getLogger(XmlDaoFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XmlDaoFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XmlDaoFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public DaoStudent getStudent() {
        try {
            // throw new UnsupportedOperationException("Not supported yet.");
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
             DocumentBuilder builder = factory.newDocumentBuilder();
             Document document = builder.parse(source);
             return new XmlDaoStudent(document);
        } catch (SAXException ex) {
            Logger.getLogger(XmlDaoFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XmlDaoFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XmlDaoFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
        
    }

    @Override
    public DaoFaculty getFaculty() {
        try {
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
             DocumentBuilder builder = factory.newDocumentBuilder();
             Document document = builder.parse(source);
             return new XmlDaoFaculty(document);
        } catch (SAXException ex) {
            Logger.getLogger(XmlDaoFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XmlDaoFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XmlDaoFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

}
